package com.efuture.collection;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @ClassNAME HashMapTest
 * @Description HashMap的基本使用方法
 * @Author zhaoshanshan
 * @Date 2021/9/7 5:01 下午
 * @Version 1.0
 */
public class HashMapTest {

    public static void main(String[] args) {
        //创建对象
        HashMap hashMap = new HashMap();
        //添加元素
        hashMap.put(1,"yifan");
        hashMap.put("2","chenhang");
        hashMap.put(3,"shanshan");
        hashMap.put(4,"tongshuai");
        //取出元素
        System.out.println(hashMap.get(1));
        System.out.println(hashMap.get(2));
        System.out.println(hashMap.get(3));
        System.out.println(hashMap.get(4));
        //遍历元素
        System.out.println("================");
        Set<Map.Entry<Object,Object>>  set = hashMap.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()){
            Map.Entry<Object,Object> node = (Map.Entry<Object,Object>)it.next();
            System.out.println(node.getKey()+"="+node.getValue());
        }
        System.out.println("================");
        for (Map.Entry<Object,Object> node:set){
            System.out.println(node.getKey()+"="+node.getValue());
        }

        System.out.println("================");
        Set setTest = hashMap.keySet();
        Iterator setTestIt =  setTest.iterator();
        while (setTestIt.hasNext()){
            Object obj = setTestIt.next();
            System.out.println(obj+"="+hashMap.get(obj));
        }
    }
}